import wx, Tkinter, tkSnack, random
from com.nasgektw.cards.client.gui.ImagePanel import ImagePanel
from com.nasgektw.cards.client.model.Card import Card
class ViewerPanel(ImagePanel):
    def __init__(self, *args, **kwds):
        ImagePanel.__init__(self, *args, **kwds)
        self.strokesLabel = wx.StaticText(self)
        self.displayTimeLabel = wx.StaticText(self)
        self.randomNumberLabel = wx.StaticText(self)
        self.pauseLabel = wx.StaticText(self)
        self.pauseLabel.SetLabel('Pause')
        self.__do_binding__()
        self.__do_layout__()
    def __do_binding__(self):
        pass
    def __do_layout__(self):
        font = wx.Font(30, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD)
        self.strokesLabel.SetForegroundColour(wx.GREEN)
        self.strokesLabel.SetFont(font)
        self.displayTimeLabel.SetForegroundColour(wx.GREEN)
        self.displayTimeLabel.SetFont(font)
        self.randomNumberLabel.SetForegroundColour(wx.GREEN)
        self.randomNumberLabel.SetFont(font)
        self.pauseLabel.SetForegroundColour(wx.GREEN)
        self.pauseLabel.SetFont(font)
        self.pauseLabel.Hide()
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.strokesLabel, (0,0), wx.DefaultSpan, wx.ALL, 0)
        sizer.Add(self.displayTimeLabel, (0,2), wx.DefaultSpan, wx.ALL | wx.ALIGN_RIGHT, 0)
        sizer.Add(self.randomNumberLabel, (2,0), wx.DefaultSpan, wx.ALL | wx.ALIGN_BOTTOM, 0)
        sizer.Add(self.pauseLabel, (2,1), wx.DefaultSpan, wx.ALL | wx.ALIGN_BOTTOM, 0)
        sizer.AddGrowableCol(0, 1)
        sizer.AddGrowableCol(1, 1)
        sizer.AddGrowableCol(2, 1)
        sizer.AddGrowableRow(0, 1)
        sizer.AddGrowableRow(1, 1)
        sizer.AddGrowableRow(2, 1)
        self.SetSizer(sizer)
class ViewerFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.panel =  ViewerPanel(self, wx.ID_ANY)
        self.currentSlide = -1
        self.deck = None
        self.slideChanger = wx.Timer(self, 100)
        self.ticker = wx.Timer(self, 200)
        self.pause = False
        self.root = Tkinter.Tk()
        tkSnack.initializeSnack(self.root) 
        self.__do_binding__()
        self.__do_layout__()
    def __do_binding__(self):
        self.Bind(wx.EVT_KEY_UP, self.__on_key_press__)
        wx.EVT_TIMER(self, 100, self.__next_slide__)
        wx.EVT_TIMER(self, 200, self.__tick__)
    def __on_key_press__(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Hide()
        if event.GetKeyCode() == wx.WXK_UP:
            self.panel.strokesLabel.SetLabel(str(int(self.panel.strokesLabel.GetLabel()) + 1))
        if event.GetKeyCode() == wx.WXK_DOWN and int(self.panel.strokesLabel.GetLabel()) > 0:
            self.panel.strokesLabel.SetLabel(str(int(self.panel.strokesLabel.GetLabel()) - 1))
        if event.GetKeyCode() == wx.WXK_RIGHT:
            self.panel.displayTimeLabel.SetLabel(str(int(self.panel.displayTimeLabel.GetLabel()) + 1))
        if event.GetKeyCode() == wx.WXK_LEFT and int(self.panel.displayTimeLabel.GetLabel()) > 1:
            self.panel.displayTimeLabel.SetLabel(str(int(self.panel.displayTimeLabel.GetLabel()) - 1))
        if event.GetKeyCode() == wx.WXK_SHIFT:
            if self.panel.randomNumberLabel.GetLabel():
                self.panel.randomNumberLabel.SetLabel('')
            else:
                self.panel.randomNumberLabel.SetLabel(str(random.randint(0, 9))+str(random.randint(0, 9))+str(random.randint(0, 9))+str(random.randint(0, 9))+str(random.randint(0, 9))+str(random.randint(0, 9)))                
        if event.GetKeyCode() == wx.WXK_SPACE:
            self.pause = not self.pause
            if self.pause:
                self.panel.pauseLabel.Show()
            else:
                self.panel.pauseLabel.Hide()
    def __do_layout__(self):
        self.panel.SetBackgroundColour('#000000')
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.panel, (0,0), wx.DefaultSpan, wx.EXPAND | wx.ALL, 0)
        sizer.AddGrowableCol(0, 1)
        sizer.AddGrowableRow(0, 1)
        self.SetSizer(sizer)
    def __next_slide__(self, event = None):
        if not self.IsShown() and self.slideChanger.IsRunning():
            self.slideChanger.Stop()
        else:
            if self.slideChanger.IsRunning():
                self.slideChanger.Stop()
            if not self.pause and len(self.deck) > self.currentSlide:
                if isinstance(self.deck[self.currentSlide + 1], Card):
                    self.panel.setImage(self.deck[self.currentSlide + 1].filePath)
                else:
                    self.panel.setImage(self.deck[self.currentSlide + 1])
                self.panel.Refresh()
                self.currentSlide += 1
                self.slideChanger.Start(int(self.panel.displayTimeLabel.GetLabel()) * 1000 + 1)
            else:
                self.slideChanger.Start(1000)
    def __tick__(self, event = None):
        if not self.IsShown():
            self.ticker.Stop()
        elif not self.pause:
            snd = tkSnack.Sound()
            filt = tkSnack.Filter('generator', 580, 30000, 0.0, 'sine', int(11500 * 0.1))
            snd.stop()
            snd.play(filter=filt, blocking=1)
            self.ticker.Start(int(float(self.panel.displayTimeLabel.GetLabel()) / float(self.panel.strokesLabel.GetLabel()) * 1000) + 1)
        else:
            self.ticker.Start(1000)
    def display(self, strokes = 0, displayTime = 1, deck = []):
        self.panel.strokesLabel.SetLabel(str(strokes))
        self.panel.displayTimeLabel.SetLabel(str(displayTime))
        self.panel.pauseLabel.Hide()
        self.panel.randomNumberLabel.SetLabel('')
        self.deck = deck
        self.currentSlide = -1
        self.ShowFullScreen(True, wx.FULLSCREEN_ALL)
        self.Layout()
        self.panel.SetFocus()
        self.__next_slide__()
        self.__tick__()